package UserAgent;
use warnings;
use HTTP::Response;
use base 'Exporter';
our @EXPORT_OK = qw(get prepare_testfile);

sub new {
    my ($CLASS, %options) = @_;
    my $self = { %options };

    bless($self,$CLASS);
    return $self;
}

sub get {
    my ($self, $url) = @_;

    $url =~ m/symbol=(.*)%20/;
    my $symbol = $1;
    my $html = `cat  $self->{file}.$symbol`;

    my $http = "200 OK HTTP/1.1\r\nContent-Length: ". length($html) ."\r\n\r\n" . $html;

    my $response = HTTP::Response->parse($http);

    return $response;

}

sub prepare_testfile {
    my ($changeto, $testfile_name) = @_;
    my $testdir = "t/tmp/";
    $testfile_name ||= "cibc.quote.html";
    if(defined $changeto) {
        my $file = $testdir . $testfile_name . "." . $changeto->{symbol};

        system("mkdir -p $testdir");
        system("cp -f t/data/$testfile_name $file");

        system("sed -i -e 's,##LAST##,$changeto->{last},g' $file");
        if($changeto->{net} >= 0) {
            system("sed -i -e 's,##CHANGE##,<img src=\"images/up_arrow.gif\">$changeto->{net},g' $file");
        } else {
            my $net = -$changeto->{net}; 
            system("sed -i -e 's,##CHANGE##,<img src=\"images/dn_arrow.gif\">$net,g' $file");
        }
        system("sed -i -e 's,##BID##,$changeto->{bid},g' $file");
        system("sed -i -e 's,##ASK##,$changeto->{ask},g' $file");
        system("sed -i -e 's/##VOLUME##/$changeto->{volume_str}/g' $file");
    }
    return $testdir . $testfile_name;
}

1;
